﻿var FCL = {
    interval : null,
    cv: null,
    ctx: null,
    Init: function (arg) {
        cv = arg;
        ctx = cv.getContext("2d");
    },
    Start: function () {
        var d1 = new Date();
        var s = d1.getSeconds();
        FCL.Draw(true);

        setTimeout(FCL.Draw, (60 - s) * 1000);
    },
    Draw: function (non) {
        var d = new Date();
        if (non == undefined && FCL.interval == null)
        {
            FCL.interval = setInterval(FCL.Draw, 60000);
        }
      
        var hour = d.getHours();
        var minute = d.getMinutes();
        FCL.Clear();

        s5 = function (n) { return n - f5() };
        s4 = function (n) { return n - f4() };
        s3 = function (n) { return n - f3() };
        s2 = function (n) { return n - f2() };
        s1 = function (n) { return n - f1() };

        is5 = function (n) {
            return s5(n) >= 0;
        };
        is4 = function (n) {
            var s = is5(n) ? s5(n) : n;
            s = s4(s);
            return s >= 0;
        };
        is3 = function (n) {
            var s = is5(n) ? s5(n) : 0;
            s = is4(n) ? s4(s) : s;
            s = s3(s);

            return s >= 0;
        };
        is2 = function (n) {
            var s = is5(n) ? s5(n) : n;
            s = is4(n) ? s4(s) : s;
            s = is3(n) ? s3(s) : s;
            s = s2(s);

            return s >= 0;
        };
        is1 = function (n) {
            var s = is5(n) ? s5(n) : n;
            s = is4(n) ? s4(s) : s;
            s = is3(n) ? s3(s) : s;
            s = is2(n) ? s2(s) : s;
            s = s1(s);
            return s >= 0;
        };

        f = function (n) {
            var fibo = [0, 1];

            if (n <= 2) return 1;

            for (var i = 2; i <= n; i++) {
                fibo[i] = fibo[i - 1] + fibo[i - 2];
            }

            return fibo[n];
        };

        f5 = function () { return f(5); };
        f4 = function () { return f(4); };
        f3 = function () { return f(3); };
        f2 = function () { return f(2); };
        f1 = function () { return f(1); };
      
        DrawRect = function (r,g,b, x,y,w,h) {
            ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
                      
            ctx.fillRect(x, y, w, h);
            ctx.lineWidth = 2;
            ctx.strokeStyle = 'yellow';
            ctx.strokeRect(x, y, w, h);

        };
        hour = hour > 12 ? hour - 12 : hour;
        var h5 = is5(hour);
        var h4 = is4(hour);
        var h3 = is3(hour);
        var h2 = is2(hour);
        var h1 = is1(hour);

        var m = ~~(minute / 5);
        var m5 = is5(m);
        var m4 = is4(m);
        var m3 = is3(m);
        var m2 = is2(m);
        var m1 = is1(m);

        var r = minute % 5;
        var r4 = is4(r);
        var r3 = is3(r);
        var r2 = is2(r);
        var r1 = is1(r);

        //C1
        DrawRect((h1 && !m1) || (!h1 && !m1) ? 255 : 0,
            (m1 && !h1) || (!h1 && !m1) ? 255 : 0,
            h1 && m1 || (!h1 && !m1) ? 255 : 0,
            (cv.width / 2) - ((cv.width / 2) / 3),
            cv.height / 4,
            cv.width / 6,
            cv.height / 4);

        //C2
        DrawRect((h2 && !m2 ) || (!h2 && !m2) ? 255 : 0,
            (m2 && !h2) || (!h2 && !m2) ? 255 : 0,
            h2 && m2 || (!h2 && !m2) ? 255 : 0,
            (cv.width / 2) - (cv.width / 2 / 3),
            0,
            cv.width / 6,
            cv.height / 4);

        //C3
        DrawRect((h3 && !m3) || (!h3 && !m3) ? 255 : 0,
            (m3 && !h3) || (!h3 && !m3) ? 255 : 0,
            h3 && m3 || (!h3 && !m3) ? 255 : 0,
            0,
            0,
            (cv.width / 6) * 2,
            (cv.height / 4) * 2);

        //C4
        DrawRect((h4 && !m4) || (!h4 && !m4) ? 255 : 0,
            (m4 && !h4) || (!h4 && !m4) ? 255 : 0,
            h4 && m4 || (!h4 && !m4) ? 255 : 0,
            0,
            cv.height / 2,
            cv.width / 2,
            cv.height / 2);

        //C5
        DrawRect((h5 && !m5) || (!h5 && !m5) ? 255 : 0,
            (m5 && !h5) || (!h5 && !m5) ? 255 : 0,
            h5 && m5 || (!h5 && !m5) ? 255 : 0,
            cv.width / 2,
            0,
            cv.width / 2,
            cv.height);
       
    },
    Clear: function () {
        ctx.clearRect(0, 0, cv.width, cv.height);
    },
    
};